
class UserModel:
    def __init__(self, id=0, name="", age=0, pwd=0):
        self.id = id
        self.name = name
        self.age = age
        self.pwd = pwd

    def __str__(self):
        return "ID为%s的学生姓名是%s,年龄是%s,密码为%s" % (self.id, self.name, self.age, self.pwd)

    def __eq__(self, other):
        return self.id == other.id


class UserController:
    def __init__(self):
        self.list_table = []  # type:list[UserModel]

    def add_user(self, new):
        """
            添加电影
        :param new: 新电影
        """
        self.list_table.append(new)

    def delete_user(self, id):
        """
            删除电影
        :param id: str类型,电影名称
        :return: bool类型,删除成功返回True,否则返回False
        """
        # for i in range(len(self.list_table)):
        #     if self.list_table[i].name == name:
        #         del self.list_table[i]
        #         return True
        # return False  # 删除失败
        target =UserModel(id)
        if target in self.list_table:
            self.list_table.remove(target)
            return True
        return False
        # 内部代码:
        # for i in range(len(self.list_table)):
        #     if self.list_table[i].__eq__(参数):
        #         del self.list_table[i]

    def modify_user(self, model: UserModel):
        """
            修改电影
        :param model: MovieModel类型,需要修改的数据
        :return: bool类型,成功返回True,否则返回False
        """
        for item in self.list_table:
            if item.id == model.id:
                # item.type = model.type
                # item.actor =model.actor
                # item.index = model.index
                item.__dict__ = model.__dict__
                return True
        return False


class UserView:
    def __init__(self):
        self.__controller = UserController()

    def __display_menu(self):
        """显示菜单"""
        print("输入1键录入学生信息")
        print("输入2键显示学生信息")
        print("输入3键删除学生信息")
        print("输入4键修改学生信息")
        print("输入5键退出")

    def __select_menu(self):
        """选择菜单"""
        number = input("请输入选项:")
        if number == "1":
            self.__input_user()
        elif number == "2":
            self.__display_user()
        elif number == "3":
            self.__remove_user()
        elif number == "4":
            self.__update_user()
        elif number == "5":
            exit()

    def __input_user(self):
        """录入电影"""
        user = UserModel(
            int(input("请输入学生ID:")),
            str(input("请输入学生姓名：")),
            int(input("请输入学生年龄:")),
            input("请输入密码:"),
        )
        self.__controller.add_user(user)

    def main(self):
        """程序入口"""
        while True:
            self.__display_menu()
            self.__select_menu()

    def __display_user(self):
        """显示所有电影"""
        for item in self.__controller.list_table:
            # print("%s的类型是%s,主演是%s,指数为%s" % (item.name, "-".join(item.type), item.actor, item.index))
            print(item)  # print(item.__str__())
            # 问题: 为什么在view类打印,去Model类重写?
            # 答：打印的对象类型是Model

    def __remove_user(self):
        while True:
            id = int(input("请输入需要删除的学生ID:"))
            if self.__controller.delete_user(id):
                print("删除学生信息成功")
                break
            else:
                print("删除学生信息失败")

    def __update_user(self):
        item = 0
        while True:
            user = UserModel(
                int(input("请输入学生ID:")),
                str(input("请输入学生姓名：")),
                int(input("请输入学生年龄:")),
                input("请输入密码:"),
            )
            item += 1
            if item==3:
                break
            elif self.__controller.modify_user(user):
                print("成功")
                break
            else:
                print("失败")


view = UserView()
view.main()